package cn.itsource.pet.controller;

import cn.itsource.config.RedisService;
import cn.itsource.pet.domain.TSearchMasterMessage;
import cn.itsource.pet.domain.ViewSearchMasterUser;
import cn.itsource.pet.query.SearchMasterMessageQuery;
import cn.itsource.pet.service.ISearchMasterService;
import cn.itsource.user.domain.TLogininfo;
import cn.itsource.utils.AjaxResult;
import cn.itsource.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/searchmaster")
public class SearchMasterController {
    @Autowired
    private ISearchMasterService searchMasterService;
    @Autowired
    private RedisService redisService;

    /**
     * 发布宠物寻主的信息
     *
     * @param masterMessage
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody TSearchMasterMessage masterMessage, HttpServletRequest request) {
        //在这里根据token获取登录的用户信息？如何取？--->如何得到token?
        String token = request.getHeader("token");
        //再从token中查到对应的用户信息
        TLogininfo logininfo = redisService.getKeyObjectValue(token, TLogininfo.class);
        return searchMasterService.publish(masterMessage,logininfo);
    }

    /**
     * 分页查询时，查询的URL没有给值
     * @param query
     * @return
     */
    @PostMapping
    public PageList<ViewSearchMasterUser> findPageByparam(@RequestBody SearchMasterMessageQuery query, HttpServletRequest request){
        String token = request.getHeader("token");//从request对象中获取header头信息中的token令牌
        return searchMasterService.findPageByParam(query,token);
    }

    @GetMapping("/shenghe")
    public AjaxResult shenghe(Long id, Integer state) {
        return searchMasterService.shenghe(id,state);
    }


}
